<?php

namespace app\api\controller\admin\setting;

use app\api\common\controller\BaseApi;
use app\api\services\admin\setting\PositionsServices;
use app\Request;
use think\App;

class PositionsController extends BaseApi
{

    private PositionsServices $services;

    public function __construct(Request $request, App $app, PositionsServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * index  显示资源列表
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function select(Request $request): \think\response\Json
    {
        $where = $request->getParams([
            ['name', '', '', 'name_like'],
            ['code'],
            ['department_id', '', '', 'pid'],
            ['delete_time', null],
        ]);

        $this->validate($where, [
            'name_like' => 'max:32',
        ], [], true, true);

        $ret = $this->services->select($where);

        return ret_value(0, "ok", $ret['list'], 200, [], ["count" => $ret['count']]);
    }

    /**
     *  显示指定资源
     *
     * @param $id
     *
     * @return \think\response\Json
     */
    public function get($id): \think\response\Json
    {
        $where = ['id' => $id];
        $ret   = $this->services->get($where, ["id, department_id,  position_code, position_name, create_time, update_time"])->toArray();
        return ret_value(0, "", $ret);
    }

    /**
     *  保存新建资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function insert(Request $request): \think\response\Json
    {
        $data    = $request->getParams([
            ['department_id', 0],
            ['position_code'],
            ['position_name'],
        ]);
        $rule    = [
            'department_id' => 'require',
            'position_code' => 'require',
            'position_name' => 'require',
        ];
        $message = [
            'department_id.require' => '关联部门不能为空',
            'position_code.require' => '岗位代码不能为空',
            'position_name.require' => '岗位名称不能为空',
        ];
        $this->validate($data, $rule, $message);
        $this->services->save($data);
        return ret_value(0, "Success");
    }

    /**
     * update  保存更新资源
     *
     * @param \app\Request $request
     * @param              $id
     *
     * @return \think\response\Json
     */
    public function update(Request $request): \think\response\Json
    {
        $where   = $request->getParams([['id']]);
        $data    = $request->getParams([
            ['id'],
            ['pid', '', '', 'department_id'],
            ['position_code'],
            ['position_name'],
        ]);
        $rule    = [
            'id'            => 'require',
            'department_id' => 'require',
            'position_code' => 'require',
            'position_name' => 'require',
        ];
        $message = [
            'department_id.require' => '关联部门不能为空',
            'position_code.require' => '岗位代码不能为空',
            'position_name.require' => '岗位名称不能为空',
        ];
        $this->validate($where, ['id' => 'require'], []);
        $this->validate($data, $rule, $message);

        $ret = $this->services->update($where, $data);
        if ($ret) {
            return ret_value(0, "Success");
        } else {
            return ret_value(-1, "Fail");
        }

    }

    /**
     * delete   删除指定资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function delete(Request $request): \think\response\Json
    {
        $data = $request->getParams([
            ['data'],
        ]);
        $this->validate($data, [
            'data' => 'require',
        ]);
        $ids = explode(',', $data['data']);
        $ret = $this->services->destroy($ids);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");

    }
}
